package com.today.springandmq.Service;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
import co.elastic.clients.elasticsearch.core.DeleteResponse;
import co.elastic.clients.elasticsearch.core.GetResponse;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient;
import co.elastic.clients.elasticsearch.indices.ExistsRequest;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import co.elastic.clients.util.ObjectBuilder;
import com.today.springandmq.Common.entity.XmBulkySupplier;
import org.springframework.beans.factory.annotation.Autowired;
import rx.functions.Func0;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class XmBulkySupplier_Iml implements XmBulkySupplier_Interface{
    private final ElasticsearchClient elasticsearchClient;

    private static final String INDEX_NAME = "XmBulkySupplier";
    public XmBulkySupplier_Iml(@Autowired ElasticsearchClient elasticsearchClient){
        this.elasticsearchClient = elasticsearchClient;
    }




    @Override
    public void createIndexIfNotExsit() {
// return new ElasticsearchIndicesClient((ElasticsearchTransport)this.transport, this.transportOptions);

        // 前面是 restclient 主要是 做一些连接 的存储 然后没了  后面 是jsonmapper
        ElasticsearchIndicesClient indices = elasticsearchClient.indices();

        BooleanResponse exists = null;
        try {
            exists = indices.exists((e) -> {

                OutSomeThing(e);
                return e.index(XmBulkySupplier_Iml.INDEX_NAME);

            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        boolean value = exists.value();

        if(!value){


            try {
                indices.create(

                        c-> c.index(XmBulkySupplier_Iml.INDEX_NAME)
                                    .mappings(
                                            m-> m.properties("id", p -> p.keyword(k -> k))
                                                        .properties("SupplierName", p->p.double_(k->k))
                                                        .properties("Test",p->p.text(k->k))
                                                        .properties("Fs_Id",p->p.alias(k->k))
                                    )



                );
            } catch (IOException e) {

                e.printStackTrace();
            }


        }


    }

    private static void OutSomeThing(ExistsRequest.Builder e) {
        System.out.println(e.getClass().getName());

        Class<?>[] interfaces = e.getClass().getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {

            System.out.println(interfaces[i].getName() + " --- --- --- --- Interface" );

        }
    }

//    public Function<TypeMapping.Builder, ObjectBuilder<TypeMapping>> fn() {
//        ObjectBuilder m;
//
//        Function function = new Function<>() {
//
//            @Override
//            public Object apply(Object o) {
//
//
//            }
//        };
//
//
//    }

    @Override
    public String indexProduct(XmBulkySupplier xmBulkySupplier) {

        IndexResponse index = null;
        try {
            index = elasticsearchClient.index(i -> i.index(XmBulkySupplier_Iml.INDEX_NAME)
                    .id(String.valueOf(xmBulkySupplier.getId()))
                    .document(xmBulkySupplier)

            );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        String id = index.id();

        return id;
    }

@Override
    public XmBulkySupplier getXmSById(String id) {
    GetResponse<XmBulkySupplier> xmBulkySupplierGetResponse = null;
    try {
     xmBulkySupplierGetResponse = elasticsearchClient.get(p -> p.index(XmBulkySupplier_Iml.INDEX_NAME).id(id), XmBulkySupplier.class


        );
    } catch (IOException e) {

        e.printStackTrace();
    }

    if (!xmBulkySupplierGetResponse.found()) {
        return null;
    }

    return xmBulkySupplierGetResponse.source();
    }

    @Override
    public List<XmBulkySupplier> ES_By_KeyWord(String keyWord) {


        List<XmBulkySupplier> collect = null;
        try {
            SearchResponse<XmBulkySupplier> supplierName = elasticsearchClient.search(s -> s.index(XmBulkySupplier_Iml.INDEX_NAME)
                    .query(
                            q -> q.multiMatch(
                                    m -> m.query(keyWord)
                                            .fields("SupplierName")
                            )

                    ), XmBulkySupplier.class);
            List<Hit<XmBulkySupplier>> hits = supplierName.hits().hits();
            collect = hits.stream().map(

                    xmBulkySupplierHit -> xmBulkySupplierHit.source()
            ).collect(Collectors.toList());


        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        if (collect == null || collect.size() == 0) {
            return Collections.emptyList();
        }
    return collect;
    }

    @Override
    public boolean delXmS(String id) throws IOException {
        DeleteResponse delete = elasticsearchClient.delete(x -> x.index(XmBulkySupplier_Iml.INDEX_NAME)
                .id(id)
        );

if(delete.result().name().equals("deleted"))
        return true;
    return false;
    };
}
